package com.itheima.controller;

import com.itheima.pojo.User;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

@RestController
@RequestMapping("/consumer")
public class UserController {
    //restTemplate相当于浏览器对象
    @Autowired
    private RestTemplate restTemplate;

    //discoveryClient对象用来发现Eureka注册中心里的服务对象
    @Autowired
    private DiscoveryClient discoveryClient;

    @HystrixCommand(fallbackMethod = "failBack")
    @GetMapping(value = "/{id}")
    public User findById(@PathVariable(value = "id") Integer id) {
        User user =restTemplate.getForObject("http://user-provider/user/"+id,User.class);
        return user;
    }


    /****
     * 服务降级处理方法
     * @return
     */
    public User failBack(Integer id){
        User user = new User();
        user.setUsername("服务降级,默认处理！");
        return  user;
    }
}
